interface IOpinionSystemLogViewModel extends IBaseViewModel<IOpinionSystemLogQueryTerms> {
    /**
    * ui-grid控制
    */
    gridOptions: uiGrid.IGridOptionsOf<systemLogListResponse>;//EContract
    /**
   * 列表行高
   */
    heightA: number;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 编号类型下拉
     */
    idTypeDropDown: ValueListItemResponse[];
    /**
     * 查询
     */
    queryFunc: () => void;

    searchUsername: (value: string) => any;

    onUserNameSelect: (data: angucomplete<EmployeeListItemResponse>) => void;
    
    handleCodeTypeChange:()=>void;

    handleTimeChange:(changeStartTime:boolean)=>void

}

interface IOpinionSystemLogQueryTerms {

    /**
     * 编号
     */
    itemCode: string,
    /**
     * 记录类型
     */
    logType: string,
    /**
     * 操作人员
     */
    userName: string,
    /**
     * 操作起始时间
     */
    startTime: any,
    /**
     * 操作结束时间
     */
    endTime: any
}

class OpinionSystemLogController extends ListController<IOpinionSystemLogViewModel, IOpinionSystemLogQueryTerms>{

    firstChange:boolean = true;

    gridApi: uiGrid.IGridOptionsOf<systemLogListResponse>;//EContractListResponse

    skip: number = 0;
    constructor($scope: IOpinionSystemLogViewModel, private valueService: IValueService, private $ngBootbox: BootboxService, private $state: angular.ui.IStateService,
        private routeService: routeService, private logService: logService, private employeeService: employeeService,private commonService:commonService) {
        super($scope)
        this.init();
    }


    init(): void {
        super.init();
        this.$scope.queryTerms = {
            logType: '0',
            itemCode: "",
            userName: "",
            startTime : "",
            endTime: "",

        };
        this.$scope.gridOptions = {
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            // useExternalSorting: true,     // 自定义排序规则
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "编号", field: 'itemCode', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: "操作人员", field: 'operatorName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: "操作时间", field: 'operatorTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: "操作记录", field: 'operatorDescription', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },// width: '11%'
            ],
            onRegisterApi: (gridApi) => {
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        }

        $('#openStartTime').click(function () {
            $('#StartTime').datetimepicker('show');
        });
        $('#openEndTime').click(function () {
            $('#EndTime').datetimepicker('show');
        });

        this.loadIdTypeDropDown();
        this.$scope.searchUsername = this.searchUsername;

        this.$scope.onUserNameSelect = this.onUserNameSelect;

        this.$scope.handleCodeTypeChange = this.handleCodeTypeChange;

        this.$scope.handleTimeChange = this.handleTimeChange;



        // this.query();


    }



    /**
     * 加载数据
     */
    loadData(queryTerms: IOpinionSystemLogQueryTerms, skip: number, count: number) {

        
        if (!this.checkQueryTerm(queryTerms)) {
            this.$scope.querying = false;
            return;
        }

        this.logService.getList(this.queryTerms.userName, this.queryTerms.itemCode, this.queryTerms.logType,  this.queryTerms.startTime, this.queryTerms.endTime,
            skip, count).then((res) => {
                // console.log(res.data)
                this.$scope.gridOptions.data = res.data.data;
                this.$scope.gridOptions.totalItems = res.data.total;
                this.$scope.heightA = (this.$scope.gridOptions.data.length * 39) + 96;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });

     
    }

    /**
     * 编号类型下拉
     */
    loadIdTypeDropDown(): void {
        this.$scope.idTypeDropDown = this.valueService.getIdTypeDropDown().data;

    }
    // 核对queryTerm
    checkQueryTerm = (queryTerms: IOpinionSystemLogQueryTerms) => {
        

        // if(!queryTerms.userName){
        //     const userName = $('#operate_userName_value').val();
        //     queryTerms.userName = userName;
        // }

        const case01 = queryTerms.logType && queryTerms.itemCode;
        const case02 = queryTerms.userName && queryTerms.startTime && queryTerms.endTime;

        if (!(case01 || case02)) {
            this.$ngBootbox.alert('需要填写一级查询条件或所有二级条件才可查询');
            return false;

        }
        // // 首位数字 订单0、应付2、应收4 总共11位
        if(case01 ){
            if( !(queryTerms.logType==='0'&&queryTerms.itemCode.charAt(0)==='0'
                || queryTerms.logType==='1'&&queryTerms.itemCode.charAt(0)==='2'
                || queryTerms.logType==='2'&&queryTerms.itemCode.charAt(0)==='4') || queryTerms.itemCode.length!==11){
                    this.$ngBootbox.alert('请输入正确的编号');
                    return false;
            }
        }

        let dateSkip = new Date(queryTerms.endTime).getTime() - new Date(queryTerms.startTime).getTime()
        if (queryTerms.startTime && queryTerms.endTime && dateSkip > 1 * 24 * 3600 * 1000) {

            this.$ngBootbox.alert('操作起始时间和操作结束时间选择时间间隔不能大于1天');
            return false;
        }

        if (queryTerms.startTime && queryTerms.endTime && dateSkip <0) {

            this.$ngBootbox.alert('操作起始时间必须在操作结束时间之前');
            return false;
        }


        let dateSkipFromNow = Date.now()- new Date(queryTerms.endTime).getTime()  ;

        if (queryTerms.startTime && dateSkipFromNow > 90 * 24 * 3600 * 1000) {
            this.$ngBootbox.alert('只可查询前90天以内的操作记录');
            return false;
        };
         

        return true;
    }

    searchUsername = (value: string) => {

        // console.log(value);
        return this.employeeService.getList(null, '', value, '', null,true,0, -1).then(res => {
            return res.data.data
        })

    }

    onUserNameSelect = (data: angucomplete<EmployeeListItemResponse>) => {
        // console.log('entering')
        if (data) {
            console.log(data)
            this.$scope.queryTerms.userName = data.description.realName;
        } else {
            this.$scope.queryTerms.userName = "";
            return;
        }
    }

    handleCodeTypeChange():void{
        // 更改编号类型后，清空原来输入的编号
        this.$scope.queryTerms.itemCode = "";
    }

    handleTimeChange =(changeStartTime:boolean)=>{
        
        if(!this.firstChange){return;}
        this.firstChange = false;

        if(changeStartTime){
            this.$scope.queryTerms.endTime = this.commonService.transformTime(new Date( new Date( this.$scope.queryTerms.startTime).getTime() +24*3600*1000),'yyyy.MM.dd HH:mm');
        }else{
            this.$scope.queryTerms.startTime = this.commonService.transformTime(new Date( new Date( this.$scope.queryTerms.endTime).getTime() -24*3600*1000),'yyyy.MM.dd HH:mm')
        }
    }
}

angular.module("tms").controller("opinionSystemLogController", ["$scope", "valueService", "$ngBootbox", "$state", "routeService", "logService", "employeeService","commonService", OpinionSystemLogController]);
